//女性角色
import {Sprite} from "../base/Sprite.js";
import {DataStore} from "../base/DataStore.js";
const screenWidth    = window.innerWidth
const screenHeight   = window.innerHeight
const rpx=DataStore.getRpx()
export class Girl extends Sprite {
    constructor() {
        super()
        this.girlsArr=[]
        //人物高度68，68
        this.width=68;
        this.height=68
        console.log('我创建了')
        // this.state='' //interview才注册事件
    }
    drawgirl1(){
        const image = Sprite.getImage('girl1');
        super.draw(image,
            0, 0,
            image.width, image.height,
            screenWidth/2-image.width/3*rpx+120*rpx ,260*rpx,
            image.width*2/3*rpx, image.height*2/3*rpx);
            this.girl1={x:screenWidth/2-image.width/3*rpx+120*rpx,y:260*rpx,name:'girl1'}
            this.girlsArr[0]=this.girl1
    }

    drawgirl2(){
        const image = Sprite.getImage('girl2');
        super.draw(image,
            0, 0,
            image.width, image.height,
            screenWidth/2-image.width/3*rpx+38*rpx ,260*rpx,
            image.width*2/3*rpx, image.height*2/3*rpx);

            this.girl2={x:screenWidth/2-image.width/3*rpx+38*rpx,y:260*rpx,name:'girl2'}
            this.girlsArr[1]=this.girl2
    }

    drawgirl3(){
        const image = Sprite.getImage('girl3');
        super.draw(image,
            0, 0,
            image.width, image.height,
            screenWidth/2-image.width/3*rpx-38*rpx ,260*rpx,
            image.width*2/3*rpx, image.height*2/3*rpx);

        this.girl3={x:screenWidth/2-image.width/3-38*rpx,y:260*rpx,name:'girl3'}
        this.girlsArr[2]=this.girl3
    }

    drawgirl4(){
        const image = Sprite.getImage('girl4');
        super.draw(image,
            0, 0,
            image.width, image.height,
            screenWidth/2-image.width/3*rpx-120*rpx ,260*rpx,
            image.width*2/3*rpx, image.height*2/3*rpx);
            // console.log(image.width*2/3)
            this.girl4={x:screenWidth/2-image.width/3*rpx-120*rpx,y:260*rpx,name:'girl4'}
            this.girlsArr[3]=this.girl4
    }

    checkIsFingerOnAir(obj) {//判断是否在人物上
        const deviation = 30
         const  a=this.girlsArr.find(val=>{
            return !!(   obj.x >= val.x - deviation
                && obj.y >= val.y - deviation
                && obj.x <= val.x + this.width + deviation
                && obj.y <= val.y + this.height + deviation  )
             })
             if(a){
                // Girl.selectGirl=a.name 
                // console.log(Girl.selectGirl)
                this.selectGirl=a.name//在director中获取不了；可能是不是同一个实例
             }

             
    }
        

    initGirlEvent(){
        this.hasInitEvent=true;//已注册事件
            DataStore.getInstance().canvas.addEventListener('touchend', ((e) => {
                e.preventDefault()
                let x = e.changedTouches[0].clientX
                let y = e.changedTouches[0].clientY
                
                this.checkIsFingerOnAir({'x':x,'y':y})
              }))
    }
    removeGirlEvent(){
        DataStore.getInstance().canvas.removeEventListener('touchend',()=>{
            console.log('移除了事件')
        })
    }

    returnGirl(){
        // console.log(this.selectGirl)
        return this.selectGirl
    }


}